<?php

/* ================================
/ A class that includes everyting
/ that is related to the system
/ of PureVote
/ =================================*/
class purevote {

	// Initialize system
	function __construct(){   
		$this->db_connect();
                session_start();
        } 

	// Destruct system
	function __destruct(){
		$this->db_close();
	}

	/* ==========================
	/  Database related Functions
	/  ==========================*/
	function db_connect() {
                $this->con = mysql_connect( 'localhost', 'username', 'password' ); // FIXME localhost, username, password
                if (!$this->con)
                        die('Could not connect: '.mysql_error());
                if (!mysql_select_db('tablename',$this->con)) //FIXME tablename
                        die("Error selecting database: ".mysql_error());
                mysql_set_charset('utf8',$this->con);
        }
	
	
	function db_select_browse_sort($order, $cols) {	//Function used at browse.php 
		$numargs = func_get_args();
		$results = mysql_query("SELECT ".$numargs[2].", ".$numargs[3].", ".$numargs[4].", ".$numargs[5].", ".$numargs[6]."
					FROM polls CROSS JOIN users
					ON polls.userid = users.userid
					ORDER BY ".$numargs[1]." ".$numargs[8]." 
					LIMIT ".$numargs[0]." OFFSET ".$numargs[7].";",$this->con);
	        for($i = 0; $array[$i] = mysql_fetch_assoc($results); $i++);
                array_pop($array);
		return $array;
	}

	function db_get_desc_name($where, $cols) { //Function used at vote.php
		$numargs = func_get_args();
		$results = mysql_query(
			"SELECT ".$numargs[1].", ".$numargs[2].", ".$numargs[3]."
			FROM polls
			WHERE pollid = ".$numargs[0].";"
			);
	        for($i = 0; $array[$i] = mysql_fetch_assoc($results); $i++);
                array_pop($array);
		return $array;
	}

	function db_get_ifvoted($where1, $where2, $cols) { //Function used at vote.php
		$numargs = func_get_args();
		$results = mysql_query(
			"SELECT ".$numargs[2]."
			FROM voted
			WHERE pollid = ".$numargs[0]." AND userid = ".$numargs[1].";"
			);
	        for($i = 0; $array[$i] = mysql_fetch_assoc($results); $i++) ;
                array_pop($array);
		return $array;
	}

	function db_get_choice_votes($where1, $where2, $cols) { //Function used at vote.php
		$numargs = func_get_args();
		$results = mysql_query(
			"SELECT ".$numargs[2].", ".$numargs[3]."
			FROM pollchoices
			WHERE pollid = ".$numargs[0]." AND num_choice = ".$numargs[1].";"
			);
	        for($i = 0; $array[$i] = mysql_fetch_assoc($results); $i++);
                array_pop($array);
		return $array;
	}
	
	function db_apply_vote($wheres) { //Function used at dovote.php
		$numargs = func_get_args();
		mysql_query(
			"UPDATE pollchoices
			SET votes = votes+1
			WHERE pollid = ".$numargs[0]." AND num_choice = ".$numargs[1].";"
		);
	}
	
	function db_set_voted($sets){
		$numargs = func_get_args();
		mysql_query(
			"INSERT INTO voted
			SET	userid = ".$numargs[0]." ,pollid = ".$numargs[1].";"
		);
	}					

        function db_close() {
                @mysql_close($this->con);
        }

}
$purevote = new purevote();

?>
